package icasue.net.base.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.*;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/4/8 下午5:48
 * @UpdateDate:
 * @Description:
 */
public class UnicodeUtil {


    /**
     * Encoder and Decoder by type of Gzip.
     * @param bytes
     * @return
     */
    public static byte[] gzip(byte[] bytes){
        byte[] gzipped = new byte[0];
        if(bytes == null || bytes.length == 0){
            return gzipped;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            GZIPOutputStream gos = new GZIPOutputStream(baos);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            byte[ ] buffer=new byte[512];
            int n;
            while((n=bais.read(buffer)) != -1){
                gos.write(buffer, 0, n);
            }
            gos.flush();
            gzipped = baos.toByteArray();
            gos.close();
        }catch (IOException e){
            gzipped = baos.toByteArray();
        }
        return gzipped;
    }


    public static byte[] unGzip(byte[] bytes){
        byte[] unGzipped = new byte[0];
        if (bytes == null || bytes.length == 0) {
            return unGzipped;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[512];
            int n;
            while ((n = ungzip.read(buffer)) != -1) {
                out.write(buffer, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(out != null) out.close();
                if(in != null) in.close();
            }catch (Throwable e){ }
        }
        return out.toByteArray();
    }


    /**
     * Encoder and Decoder by type of deflate.
     * @param bytes
     * @return
     */
    public static byte[] deflate(byte[] bytes) {
        byte[] deflated = new byte[0];
        if (bytes == null || bytes.length == 0) {
            return deflated;
        }
        int len = 0;
        Deflater defl = new Deflater();
        defl.setInput(bytes);
        defl.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] outputByte = new byte[512];
        try {
            while (!defl.finished()) {
                len = defl.deflate(outputByte);
                bos.write(outputByte, 0, len);
            }
            defl.end();
            deflated = bos.toByteArray();
        } catch (Exception e) {
            deflated = bos.toByteArray();
        } finally {
            try {
                if(bos != null) bos.close();
            }catch (Throwable e){ }
        }
        return deflated;
    }

    public static byte[] unDeflate(byte[] bytes) {
        byte[] unDeflated = new byte[0];
        if (bytes == null || bytes.length == 0) {
            return unDeflated;
        }
        int len = 0;
        Inflater infl = new Inflater();
        infl.setInput(bytes);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] outByte = new byte[512];
        try {
            while (!infl.finished()) {
                len = infl.inflate(outByte);
                if (len == 0) { break; }
                bos.write(outByte, 0, len);
            }
            infl.end();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(bos != null) bos.close();
            }catch (Throwable e){ }
        }
        return bos.toByteArray();
    }

}
